package com.sc.smartcommunity.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.sc.smartcommunity.common.Result;
import com.sc.smartcommunity.domain.PaymentRecord;
import com.sc.smartcommunity.domain.User;
import com.sc.smartcommunity.domain.UserAddress;
import com.sc.smartcommunity.service.PaymentRecordService;
import com.sc.smartcommunity.service.UserAddressService;
import com.sc.smartcommunity.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chenke Zhao
 * @date 2025/9/10 19:08
 * @description SysController
 */
@RestController
@RequestMapping("/sys")
@Tag(name = "我的页面 管理", description = "提供我的页面的接口")
public class SysController {

    @Resource
    private UserService userService;

    @Resource
    private PaymentRecordService paymentRecordService;

    @Resource
    private UserAddressService userAddressService;

    @Operation(summary = "获取用户信息", description = "必须登录后才能访问")
    @GetMapping("/profile")
    @SaCheckLogin
    public Result<User> profile() {
        String loginId = StpUtil.getLoginIdAsString();
        User userInfo = userService.getUserInfo(loginId);
        return Result.success(userInfo);
    }

    @Operation(summary = "收获地址列表", description = "必须登录后才能访问")
    @GetMapping("/addressList")
    @SaCheckLogin
    public Result<List<UserAddress>> addressList() {
        String loginId = StpUtil.getLoginIdAsString();
        List<UserAddress> addressList = userAddressService.getAddressList(loginId);
        return Result.success(addressList);
    }

    @Operation(summary = "添加收获地址", description = "必须登录后才能访问")
    @PostMapping("/address")
    @SaCheckLogin
    public Result<String> addAddress(@RequestBody UserAddress address) {
        boolean success = userAddressService.addAddress(address);
        return success ? Result.success("添加成功") : Result.error("添加失败");
    }

    @Operation(summary = "删除收获地址", description = "必须登录后才能访问")
    @DeleteMapping("/address/{id}")
    @SaCheckLogin
    public Result<String> deleteAddress(@PathVariable Long id) {
        boolean success = userAddressService.deleteAddress(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "修改收获地址", description = "必须登录后才能访问")
    @PutMapping("/address")
    @SaCheckLogin
    public Result<String> updateAddress(@RequestBody UserAddress address) {
        boolean success = userAddressService.updateAddress(address);
        return success ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Operation(summary = "设置默认收获地址", description = "必须登录后才能访问")
    @PutMapping("/address/default")
    @SaCheckLogin
    public Result<String> setDefaultAddress(@RequestParam Long id) {
        boolean success = userAddressService.setDefaultAddress(id);
        return success ? Result.success("设置成功") : Result.error("设置失败");
    }


    @Operation(summary = "获取缴费记录", description = "必须登录后才能访问")
    @GetMapping("/paymentRecords")
    @SaCheckLogin
    public Result<List<?>> paymentRecords() {
        String loginId = StpUtil.getLoginIdAsString();
        List<PaymentRecord> paymentRecords = paymentRecordService.getPaymentRecords(loginId);
        return Result.success(paymentRecords);
    }

    @Operation(summary = "修改用户信息", description = "必须登录后才能访问")
    @PutMapping("/user")
    @SaCheckLogin
    public Result<String> paymentRecord(@RequestBody User user) {
        boolean success = userService.updateUser(user);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

}
